package com.gsk.europepharma.salesReport.struts.plugin;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.RequestProcessor;

/**
 * @author mqf31318
 *
 */
public class SecureRequestProcessor extends RequestProcessor {

	private static final Log log = LogFactory
			.getLog(SecureRequestProcessor.class);
	/**
	 * 
	 */
	protected boolean processPreprocess(HttpServletRequest request,
			HttpServletResponse response) {
		//
		if (log.isDebugEnabled()) {
			log.debug("enter processPreprocess:");
		}
		//
		// Recuperation du UserPrincipal instancie par le mecanisme
		// Container/authentification/SED/Tomcat
		Principal user = request.getUserPrincipal();
		HttpSession session = request.getSession();
		//
		if (user == null) {
			// utilisateur non authentifie
			try {
				response.sendError(HttpServletResponse.SC_BAD_REQUEST);
			} catch (IOException ex) {
				ex.printStackTrace();
				log.error("SendRedirectError : ", ex);
			}
			return false;
		} else {
			// Utilisateur authentifie
			session.setAttribute("login", user.getName());
		}
		return true;
	}

	
}
